package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
public class SetmealServiceImpl implements SetmealService {

    private final SetmealMapper setmealMapper;
    private final SetmealDishMapper setmealDishMapper;
    private final DishMapper dishMapper;

    @Autowired
    public SetmealServiceImpl(SetmealMapper setmealMapper, SetmealDishMapper setmealDishMapper, DishMapper dishMapper) {
        this.setmealMapper = setmealMapper;
        this.setmealDishMapper = setmealDishMapper;
        this.dishMapper = dishMapper;
    }

    @Override
    @Transactional
    public void saveWithSetmealDish(SetmealDTO setmealDTO) {
        //插入套餐数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);

        //填充套餐id数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }

        //批量插入套餐菜品关系数据
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        Setmeal setmeal = new Setmeal();
        //TODO_over copy出错
        Integer categoryId = setmealPageQueryDTO.getCategoryId();
        if(categoryId!=null){
            setmeal.setCategoryId(Long.valueOf(categoryId));
        }
        setmeal.setName(setmealPageQueryDTO.getName());
        setmeal.setStatus(setmealPageQueryDTO.getStatus());
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> pages = setmealMapper.pageQuery(setmeal);
        return new PageResult(pages.getTotal(), pages.getResult());
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithDish(List<Long> ids) {
        //判断售卖状态，若有起售则不能删除
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if(Objects.equals(setmeal.getStatus(), StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException("请先停售套餐！");
            }
        }

        //删除setmeal
        setmealMapper.deleteByIds(ids);

        //删除关联数据setmealDIsh
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    @Override
    @Transactional
    public SetmealVO getWithDishById(Long id) {
        //查询setmeal,categoryName
        SetmealVO setmealVO = setmealMapper.getWithCategoryNameById(id);

        //查询setmealDish
        List<SetmealDish> dishes = setmealDishMapper.getBySetmealId(id);

        //封装成setmealVO
        setmealVO.setSetmealDishes(dishes);

        return setmealVO;
    }

    @Override
    @Transactional
    public void updateWithDishes(SetmealDTO setmealDTO) {
        //更新setmeal
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        //更新setmealDish--删除原有dish
        setmealDishMapper.deleteBySetmealIds(List.of(setmealDTO.getId()));

        //更新setmealDish--重新插入dish
        //TODO_over bug 填充setmealDish的categoryId
        List<SetmealDish> dishes = setmealDTO.getSetmealDishes();
        for (SetmealDish dish : dishes) {
            dish.setSetmealId(setmealDTO.getId());
        }
        setmealDishMapper.insertBatch(dishes);
    }

    @Override
    public void setStatus(Integer status, Long id) {
        //判断套餐包含的菜品是否停售，若是则不能将套餐设置在售
        if(Objects.equals(status, StatusConstant.ENABLE)){
//            List<SetmealDish> dishes = setmealDishMapper.getBySetmealId(id);
            //优化：根据setmealId查询dish
            List<Dish> dishes = dishMapper.getBySetmealId(id);

            //对dishes判空
            if(dishes==null||dishes.isEmpty()){
                throw new BaseException("套餐内容为空");
            }

            for (Dish dish : dishes) {
                if(Objects.equals(dish.getStatus(), StatusConstant.DISABLE)){
                    throw new SetmealEnableFailedException("请先启售菜品");
                }
            }
        }

        //一般情况
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
